package com.cs4261.smartalarm.backend;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * @author Joshua
 *
 */
public class AlarmsDatabaseHelper {
	private SQLiteDatabase db;
	private MySQLiteHelper dbHelper;
	private String[] columnNames = {MySQLiteHelper.COLUMN_ID, MySQLiteHelper.COLUMN_NAME};

	/**
	 * @param context
	 */
	public AlarmsDatabaseHelper(Context context) {
		Log.d("AlarmsDatabaseHelper", "Inside constructor");
		dbHelper = new MySQLiteHelper(context);
		Log.d("AlarmsDatabaseHelper", "Finishing constructor");
	}

	/**
	 * @throws SQLException
	 */
	public void open() throws SQLException {
		db = dbHelper.getWritableDatabase();
	}

	/**
	 * 
	 */
	public void close() {
		dbHelper.close();
	}

	/**
	 * @param name
	 * @return
	 */
	public Alarm createAlarm(String name) {
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.COLUMN_NAME, name);
		long insertId = db.insert(MySQLiteHelper.TABLE_ALARMS, null, values);
		Cursor cursor = db.query(MySQLiteHelper.TABLE_ALARMS, columnNames,
				MySQLiteHelper.COLUMN_ID + " = " + insertId, null, null, null,
				null);
		cursor.moveToFirst();
		Alarm newAlarm = cursorToAlarm(cursor);
		cursor.close();
		return newAlarm;
	}

	/**
	 * @param id
	 * @return
	 */
	public Alarm getAlarmById(long id) {
		
		Cursor cursor = db.query(MySQLiteHelper.TABLE_ALARMS, columnNames,
				MySQLiteHelper.COLUMN_ID + " = " + id, null, null, null,
				null);
		cursor.moveToFirst();
		Alarm newAlarm = cursorToAlarm(cursor);
		cursor.close();
		return newAlarm;
	}

	/**
	 * @param alarm
	 */
	public void updateAlarm(Alarm alarm) {
		long id = alarm.getId();
		String name = alarm.getName();
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.COLUMN_NAME, name);
		
		db.update(MySQLiteHelper.TABLE_ALARMS, values, MySQLiteHelper.COLUMN_ID + " = " + id, null);
	}

	/**
	 * @param alarm
	 */
	public void deleteAlarm(Alarm alarm) {
	    long id = alarm.getId();
	    System.out.println("Comment deleted with id: " + id);
	    db.delete(MySQLiteHelper.TABLE_ALARMS, MySQLiteHelper.COLUMN_ID + " = " + id, null);
	}

	/**
	 * @return
	 */
	public List<Alarm> getAllAlarms() {
		List<Alarm> alarms = new ArrayList<Alarm>();
		Cursor cursor = db.query(MySQLiteHelper.TABLE_ALARMS, columnNames,
				null, null, null, null, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Alarm alarm = cursorToAlarm(cursor);
			alarms.add(alarm);
			cursor.moveToNext();
		}
		cursor.close();
		return alarms;
	}

	/**
	 * @param cursor
	 * @return
	 */
	private Alarm cursorToAlarm(Cursor cursor) {
		Alarm alarm = new Alarm(cursor.getLong(0), cursor.getString(1));
		return alarm;
	}
}